// utils.js
(function () {
    /** 引入依赖包 */
    var preURL = "https://gitee.com/micoua/tmjsb/raw/master";
    var dependencyURLs = {
        fileSaver: { script: { fileSaver: preURL + "/api/fileSaver.js" } }
    };

    Utils = {
        JSON: {
            insertJsonFile: function (path, params) {
                InsertHtml.parseURL(dependencyUrls.fileSaver);
                var jsonObj = this.queryJsonFile(path);
                for (var key in params) { jsonObj[key] = params[key]; }
                if (Object.keys(jsonObj).length == 0) { return; }
                saveAs(new File([JSON.stringify(jsonObj)], { type: "" }), path.substring(path.lastIndexOf("/") + 1, path.length));
            },

            deletedJsonFile: function (path, params) {
                var jsonObj = this.queryJsonFile(path);
                for (var key in params) { delete jsonObj[key]; }
                this.insertJsonFile(path, {});
            },

            queryJsonFile: function (path) {
                return Utils.Http.getJSON(path);
            },
        },
        Http: {
            getJSON: function (path) { return this.ajaxHttp(path, "GET", null, "JSON", false); },

            /** 通过Ajax请求获取数据
             *  @param url 请求路径
             *  @param type 请求类型, GET/POST
             *  @param data 请求参数
             *  @param dataType 相应数据类型, JSON/JSONP
             *  @param async 是否异步请求, true/false
             */
            ajaxHttp: function (url, type, data, dataType, async) {
                var callback;
                $.ajax({ url: url, type: type, data: data, dataType: dataType, async: async, success: function (result) { callback = result; } });
                return callback;
            }
        },
        GMData: {
            GM_INIT: function () {
                var GM_datas = {};

                function GM_getValue(key) { return GM_datas[key]; }

                function GM_setValue(key, data) { GM_datas[key] = data; }
            }
        },
        Maction: {
            /** 显隐元素
             *  @param  ele 需要显隐元素的选择器填充内容(如 #id 名、.class 名)
             *  @param  changeTime 透明度变化的秒数(数值即可)
             *  @param  destOpacity 透明度目标数值(小数值即可)
             */
            showOrHiddenEle: function (ele, changeTime, destOpacity) {
                var currentOpacity = tools().getNum($(ele).css("opacity"));
                if (currentOpacity === destOpacity) {
                    $(ele).css({ transition: "all " + changeTime + "s", visibility: "hidden", opacity: 0 }); // 隐藏元素
                } else {
                    $(ele).css({ transition: "all " + changeTime + "s", visibility: "visible", opacity: destOpacity }); // 显示元素
                }
            },

            /** 透明度动作
             *  @param  ele 需要改变透明度的元素的选择器填充内容(如 #id 名、.class 名)
             *  @param  changeTime 透明度变化的秒数(数值即可)
             *  @param  destOpacity 透明度目标数值(小数值即可)
             */
            opacityAnimation: function (ele, changeTime, destOpacity) {
                $(ele).css({ transition: "opacity " + changeTime + "s", opacity: destOpacity });
            },

            /** 位移动作 
             *  @param  ele 需要位移的元素的选择器填充内容(如 #id 名、.class 名)
             *  @param  changeTime 位移的秒数(数值即可)
             *  @param  changeRange_horizontal 水平位移元素的距离(纯数字即可, 正数往右, 负数往左)
             *  @param  changeRange_vertical 垂直位移元素的距离(纯数字即可, 正数往下, 负数往上)
             */
            moveAnimation: function (ele, changeTime, changeRange_horizontal, changeRange_vertical) {
                // 水平移动
                if (changeRange_horizontal != null && changeRange_vertical == null) {
                    $(ele).css({ transition: "transform " + changeTime + "s", transform: "translateX(" + changeRange_horizontal + "px)" });
                }

                // 垂直移动
                if (changeRange_horizontal === null && changeRange_vertical != null) {
                    $(ele).css({ transition: "transform " + changeTime + "s", transform: "translateY(" + changeRange_vertical + "px)" });
                }

                // 任意移动
                if (changeRange_horizontal != null && changeRange_vertical != null) {
                    $(ele).css({ transition: "transform " + changeTime + "s", transform: "translate(" + changeRange_horizontal + "px," + changeRange_vertical + "px)" });
                }
            },

            /** 拖拽动作 
             *  @param  ele 需要拖拽的元素的选择器填充内容(如 #id 名、.class 名)
             */
            dragElement: function (ele) {
                $(ele).mousedown(function (e) {
                    var positionDiv = $(this).offset();
                    var distenceX = e.pageX - positionDiv.left;
                    var distenceY = e.pageY - positionDiv.top;
                    var eleTransition = $(ele).css('transition');

                    $(document).mousemove(function (e) {
                        var x = e.pageX - distenceX;
                        var y = e.pageY - distenceY;

                        if (x < 0) {
                            x = 0;
                        } else if (x > $(document).width() - $(ele).outerWidth(true)) {
                            x = $(document).width() - $(ele).outerWidth(true);
                        }

                        if (y < 0) {
                            y = 0;
                        } else if (y > $(document).height() - $(ele).outerHeight(true)) {
                            y = $(document).height() - $(ele).outerHeight(true);
                        }

                        $(ele).css({
                            'left': x + 'px',
                            'top': y + 'px',
                            'transition': 'all 0s'
                        });
                    });

                    $(document).mouseup(function () {
                        $(document).off('mousemove');
                        $(ele).css('transition', eleTransition);
                    });
                });
            }
        }
    };
})();